/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import java.util.Iterator;

public class QualifiedName extends TreeNode {

      /**
     * index of id in child list
     */
    private static final int ID_INDEX = 0;

    /**
     * index of path in child list
     */
    private static final int PATH_INDEX = 1;

    public QualifiedName(Location location_, Id id_, Path path_) {
        super(location_, id_, path_);
    }


    public QualifiedName(Id id_, Path path_) {
        super(Location.UNKNOWN, id_, path_);
    }



    public QualifiedName(Location location_, Id id_, QualifiedName namePath_) {
        super(location_, id_);
        // take the QualifiedName and make it the path of this
        Ids pathIds = new Ids();
        Iterator oldPathIds = namePath_.getPath().getPackages().getIdIter();
        while (oldPathIds.hasNext()) {
            pathIds.addId((Id)oldPathIds.next());
        }
        pathIds.addId(namePath_.getId());
        Path path = new Path(Location.UNKNOWN, pathIds);
        addChild(path);
    }


    public QualifiedName(Id id_, QualifiedName namePath_) {
        super(Location.UNKNOWN, id_);
        // take the QualifiedName and make it the path of this
        Ids pathIds = new Ids();
        Iterator oldPathIds = namePath_.getPath().getPackages().getIdIter();
        while (oldPathIds.hasNext()) {
            pathIds.addId((Id)oldPathIds.next());
        }
        pathIds.addId(namePath_.getId());
        Path path = new Path(Location.UNKNOWN, pathIds);
        addChild(path);
    }


    //GET AND SET METHODS

    /**
     * gets the id of this
     * @return the {@link Id}
     */
    public Id getId() {
        return (Id)childAt(ID_INDEX);
    }

    /** set the Id of this */
    public void setId(Id newChild_) {
        setChild(ID_INDEX, newChild_);
    }

    /**
     * gets the path of this
     * @return the {@link Path}
     */
    public Path getPath() {
        return (Path)childAt(PATH_INDEX);
    }

    /** set the Path of this */
    public void setPath(Path newChild_) {
        setChild(PATH_INDEX, newChild_);
    }

    /**
     * @return true if this has an empty path
     */
    public boolean hasEmptyPath() {
        return !(getPath().getPackages().getIdIter().hasNext());
    }

    /**
     * accept an AST visitor
     * @param visitor_ the visitor
     */
    protected void acceptVisitor(ASTVisitor visitor_) {
        visitor_.visit(this);
    }

    /**
     * accept a returning AST visitor
     * @param visitor_ the visitor
     * @return the Object returned by the visitor
     */
    protected Object acceptReturnVisitor(ASTReturnVisitor visitor_) {
        return visitor_.visit(this);
    }

    /**
     * @param obj the object for comparison
     * @return <pre>true</pre> if this name is the same as obj; <pre>false</pre> otherwise
     */
    public boolean equals(Object obj) {
        if (obj instanceof QualifiedName) {
            QualifiedName other = (QualifiedName)obj;
            if (getId().equals(other.getId()) &&
                getPath().equals(other.getPath())) {
                return true;
            }
        }
        return false;
    }

    public int hashCode() {
        return getId().hashCode() + getPath().hashCode();
    }
    
    public String nodeString() {
        // NOTE: took out chooser code, need another flag
        // to specify no fully qualified names
        if (hasEmptyPath()) {
            return getId().nodeString();
        } else {
            return getPath() + "/" + getId();
        }
    }

    public String toString()
    {
        return nodeString();
    }
}




















